﻿using System;
using System.IO;

namespace MSPE.Public.FOSS.AutoStatusPlus.Common
{
    internal static class EmbeddedResource
    {
        internal static StreamReader GetStream(System.Reflection.Assembly assembly, string name)
        {
            foreach (string _resName in assembly.GetManifestResourceNames())
            {
                if (_resName != null)
                    if (_resName.EndsWith(name))
                    {
                        Stream _rs = assembly.GetManifestResourceStream(_resName);

                        if (_rs != null)
                            return new StreamReader(_rs);
                    }
            }
            return null;
        }

        internal static string GetString(System.Reflection.Assembly assembly, string name)
        {
            StreamReader _sr = null;

            try
            {
                _sr = GetStream(assembly, name);
                string _data = _sr.ReadToEnd();
                return _data;
            }
            
            finally
            {
                if (_sr != null) _sr.Close();
            }
        }

        internal static string GetString(string name)
        {
            return GetString(typeof(EmbeddedResource).Assembly, name);
        }
    }
}
